<?php

namespace App\Services\MakeMoment;

use App\Models\User;
use App\Models\Moment;
use App\Models\MomentLike;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LikeMoment
{
	protected $user;
	
	protected $moment;
	
	public function __construct(User $user, Moment $moment)
	{
		$this->user = $user;
		$this->moment = $moment;
	}
	
	
	
	protected function like()
	{
		$moment = DB::transaction(function () {
			
			$momentLike = $this->user->momentLikes->first();
			if($momentLike){
				$momentLike->delete();
				$moment = $this->moment->decrement('likes',1);
			}else{
				$this->user->momentLikes()->create(['moment_id'=>$this->moment->id]);
				$moment = $this->moment->increment('likes',1);
			}
			

			return $moment;
		});
		
		return $moment;
	}
	
	public function make()
	{
		return $this->like();
	}
}
